<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 微信投诉类
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   allen.qiang
 * @version 1.0
 */


class wechatVote {
	
    private $dbca;
	
    /**
     * 构造函数
     * @param DBCA $dbca 数据库对象
     */
    public function __construct( $dbca ) {
        $this->dbca = $dbca;
    }
	
    /**
     * 查询投票信息
     * @param array $arg_array 查询参数数组
     * $arg_array = array (
     *      'sortname' => string 排序字段
     *      'sortorder' => string 排序方式
     *      'page' => int 分页页码
     *      'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchVote( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //投票信息ID
        if ( isset($arg_array['voteID']) && is_numeric($arg_array['voteID']) ) {
            $sql .= ' AND voteID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['voteID'];
        }

        //投票标题
        if ( isset($arg_array['voteTitle']) && !empty($arg_array['voteTitle']) ) {
            $sql .= ' AND voteTitle = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['voteTitle'];
        }

        //投票信息审核状态
        if ( isset($arg_array['voteStatus']) && is_numeric($arg_array['voteStatus']) ) {
            $sql .= ' AND voteStatus = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['voteStatus'];
        }
        
        //开始日期
        if ( isset($arg_array['startDate']) && !empty($arg_array['startDate'])) {
        	$sql .= ' AND startDate <= ?';
            $params_type .= 's';
            $params_array[] = $arg_array['startDate'];
        }
        
        //结束日期
        if ( isset($arg_array['endDate']) && !empty($arg_array['endDate'])) {
        	$sql .= ' AND endDate >= ?';
            $params_type .= 's';
            $params_array[] = $arg_array['endDate'];
        }
		
        //使用状态
        if ( isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg']) ) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if ( $sql ) {
            $sql = 'WHERE ' . substr($sql, 4);
        }
		
        try {

            if ( isset($arg_array['rp']) && $arg_array['rp'] > 1 ) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM wechat_vote ' . $sql;

                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if ( isset($arg_array['sortname']) && !empty($arg_array['sortname']) )
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if ( !isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0 )
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if ( isset($arg_array['sortname']) && !empty($arg_array['sortname']) )
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM wechat_vote ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'voteID' 	  => $item->voteID,
                    'voteTitle' 	  => $item->voteTitle,
                    'startDate' 	  => $item->startDate,
                    'endDate' 	  => $item->endDate,
                    'voteNum' 	  => $item->voteNum,
                    'voteStatus' 	  => $item->voteStatus,
                    'useFlg' 	  => $item->useFlg
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 查询投票主题信息
     * @param array $arg_array 查询参数数组
     * $arg_array = array (
     *      'sortname' => string 排序字段
     *      'sortorder' => string 排序方式
     *      'page' => int 分页页码
     *      'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchVoteTheme( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //投票信息ID
        if ( isset($arg_array['voteID']) && !empty($arg_array['voteID']) && is_numeric($arg_array['voteID']) ) {
            $sql .= ' AND voteID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['voteID'];
        }

        //投票主题ID
        if ( isset($arg_array['themeID']) && !empty($arg_array['themeID']) && is_numeric($arg_array['themeID']) ) {
            $sql .= ' AND themeID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['themeID'];
        }

        //投票标题
        if ( isset($arg_array['themeName']) && !empty($arg_array['themeName']) ) {
            $sql .= ' AND themeName = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['themeName'];
        }

        if ( $sql ) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if ( isset($arg_array['rp']) && $arg_array['rp'] > 1 ) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM wechat_vote_theme ' . $sql;

                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if ( isset($arg_array['sortname']) && !empty($arg_array['sortname']) )
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if ( !isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0 )
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if ( isset($arg_array['sortname']) && !empty($arg_array['sortname']) )
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM wechat_vote_theme ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'voteID' 	  => $item->voteID,
                    'themeID' 	  => $item->themeID,
                    'themeName' 	  => $item->themeName,
                    'canMulti' 	  => $item->canMulti,
                    'isMust' 	  => $item->isMust,
                    'themeNum' 	  => $item->themeNum
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 查询投票主题选项信息
     * @param array $arg_array 查询参数数组
     * $arg_array = array (
     *      'sortname' => string 排序字段
     *      'sortorder' => string 排序方式
     *      'page' => int 分页页码
     *      'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchVoteThemeOption( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //投票信息ID
        if ( isset($arg_array['voteID']) && !empty($arg_array['voteID']) && is_numeric($arg_array['voteID']) ) {
            $sql .= ' AND voteID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['voteID'];
        }

        //投票主题ID
        if ( isset($arg_array['themeID']) && !empty($arg_array['themeID']) && is_numeric($arg_array['themeID']) ) {
            $sql .= ' AND themeID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['themeID'];
        }

        //投票选项名称
        if ( isset($arg_array['optionName']) && !empty($arg_array['optionName']) ) {
            $sql .= ' AND optionName = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['optionName'];
        }

        if ( $sql ) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if ( isset($arg_array['rp']) && $arg_array['rp'] > 1 ) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM wechat_vote_theme_option ' . $sql;

                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if ( isset($arg_array['sortname']) && !empty($arg_array['sortname']) )
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if ( !isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0 )
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if ( isset($arg_array['sortname']) && !empty($arg_array['sortname']) )
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM wechat_vote_theme_option ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'voteID' 	  => $item->voteID,
                    'themeID' 	  => $item->themeID,
                    'optionID' 	  => $item->optionID,
                    'optionName' 	  => $item->optionName,
                    'optionNum' 	  => $item->optionNum,
                    'optionSortby' 	  => $item->optionSortby,
                    'optionImg' 	  => $item->optionImg
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 查询会员提交的投票选项详情
     * @param array $arg_array 查询参数数组
     * $arg_array = array (
     *      'sortname' => string 排序字段
     *      'sortorder' => string 排序方式
     *      'page' => int 分页页码
     *      'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchVoteCustDetail( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //投票信息ID
        if ( isset($arg_array['voteID']) && !empty($arg_array['voteID']) && is_numeric($arg_array['voteID']) ) {
            $sql .= ' AND voteID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['voteID'];
        }
        
        //投票主题ID
        if ( isset($arg_array['themeID']) && !empty($arg_array['themeID']) && is_numeric($arg_array['themeID']) ) {
            $sql .= ' AND themeID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['themeID'];
        }

        //投票选项名称
        if ( isset($arg_array['optionName']) && !empty($arg_array['optionName']) ) {
            $sql .= ' AND optionName = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['optionName'];
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ( $sql ) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if ( isset($arg_array['rp']) && $arg_array['rp'] > 1 ) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM wechat_vote_cust_detail ' . $sql;

                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if ( isset($arg_array['sortname']) && !empty($arg_array['sortname']) )
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if ( !isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0 )
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if ( isset($arg_array['sortname']) && !empty($arg_array['sortname']) )
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM wechat_vote_cust_detail ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'uuid' 	  		=> 	$item->uuid,
                    'timeStamp' 	=> 	$item->timeStamp,
                    'creator' 	  	=> 	$item->creator,
                    'createTime' 	=> 	$item->createTime,
                    'qcID' 	  		=> 	$item->qcID,
                    'voteID' 	  	=> 	$item->voteID,
                    'themeID' 	  	=> 	$item->themeID,
                    'optionID' 	  	=> 	$item->optionID,
                    'optionName' 	=> 	$item->optionName
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

	    
    /**
     * 查询会员提交的投票信息
     * @param array $arg_array 查询参数数组
     * $arg_array = array (
     *      'sortname' => string 排序字段
     *      'sortorder' => string 排序方式
     *      'page' => int 分页页码
     *      'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchVoteCust( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //投票信息ID
        if ( isset($arg_array['voteID']) && is_numeric($arg_array['voteID']) ) {
            $sql .= ' AND voteID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['voteID'];
        }
		
        //会员号
        if ( isset($arg_array['pcustID']) && is_numeric($arg_array['pcustID']) ) {
            $sql .= ' AND pcustID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['pcustID'];
        }
        

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ( $sql ) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if ( isset($arg_array['rp']) && $arg_array['rp'] > 1 ) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM wechat_vote_cust ' . $sql;

                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if ( isset($arg_array['sortname']) && !empty($arg_array['sortname']) )
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if ( !isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0 )
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if ( isset($arg_array['sortname']) && !empty($arg_array['sortname']) )
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM wechat_vote_cust ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'qcID' 	  => $item->qcID,
                    'voteID' 	  => $item->voteID,
                    'pcustID' 	  => $item->pcustID,
                    'suggest' 	  => $item->suggest,
                    'mobile' 	  => $item->mobile,
                    'custName' 	  => $item->custName
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }
    
    /**
	 * 问卷调查保存
	 * @param array $arg_array 查询参数数组
     * @param array $arg_array 参数数组
     * @return null
	 */
	public function saveVote(&$arg_array) {
		
		if(!isset($arg_array['creator'],$arg_array['voteName'],$arg_array['startDate'],$arg_array['endDate'],$arg_array['useFlg'],$arg_array['theme'])){
			return array('code' => 400,'message' => '参数错误');
		}

		if(empty($arg_array['voteName'])){
			return array('code' => 400, 'message' => '投票名称不能为空');
		}

		if(empty($arg_array['startDate'])){
			return array('code' => 400, 'message' => '开始日期不能为空');
		}

		if(empty($arg_array['endDate'])){
			return array('code' => 400, 'message' => '结束日期不能为空');
		}
		
		if($arg_array['startDate'] > $arg_array['endDate']){
			return array('code' => 400, 'message' => '开始日期不能大于结束日期');
		}
	
		if(!in_array($arg_array['useFlg'], array(0, 1))){
			$arg_array['useFlg'] = 1;
		}
		
		if (count($arg_array['theme']) == 0) {
			return array('code' => 400, 'message' => '没有设置投票主题');
		}
		
		$voteID = 0;
		
		try{

			//开始事务
			$this->dbca->beginTransaction();
			
			$wechat_vote = new entity('wechat_vote');
			if(!empty($arg_array['voteID'])) {
				
				//检查投票信息
				$wechat_vote->voteID = $arg_array['voteID'];
				if(!$this->dbca->fetch($wechat_vote)){
					throw new Exception('投票信息没有找到', 404);
				}
				
				//投票信息状态
				if ($wechat_vote->voteStatus == 1) {
					throw new Exception('投票信息已审核，无法修改', 404);
				}
				
				$voteID = $wechat_vote->voteID;
					
				//删除投票主题
				$delete_sql = 'DELETE FROM wechat_vote_theme WHERE voteID = ?';
				$this->dbca->stmt_query($delete_sql, 'i', $voteID);
				
				//删除投票选项
				$delete_sql = 'DELETE FROM wechat_vote_theme_option WHERE voteID = ?';
				$this->dbca->stmt_query($delete_sql, 'i', $voteID);
				
			} else {
				$wechat_vote->creator = $arg_array['creator'];
			}

			//保存或更新问卷主表信息
			$wechat_vote->voteTitle = $arg_array['voteName'];
			$wechat_vote->startDate = $arg_array['startDate'];
			$wechat_vote->endDate = $arg_array['endDate'];
			$wechat_vote->useFlg = $arg_array['useFlg'];
			$wechat_vote->voteStatus = 0;
			$result = $this->dbca->save($wechat_vote);
			if ($result) {
				if ($voteID == 0) {
					$voteID = $this->dbca->insert_id;
				}
			} else {
				throw new Exception('投票信息保存失败，请重试或联系系统管理员', 404);
			}
			
			//保存投票主题和选项
			foreach($arg_array['theme'] as $key => &$vote) {
				$key += 1;
				//检查投票信息
				if (!isset($vote['themeName']) || empty($vote['themeName'])) {
					throw new Exception(sprintf('"投票主题%d"，主题名不能为空', $key), 400);
				}
				if (!isset($vote['canMulti']) || !in_array($vote['canMulti'], array(0, 1))) {
					throw new Exception(sprintf('"投票主题%d"，请选择是否可以多选', $key), 400);
				}
				if (!isset($vote['isMust']) || !in_array($vote['isMust'], array(0, 1))) {
					throw new Exception(sprintf('"投票主题%d"，请选择是否是否必选', $key), 400);
				}
				if (!isset($vote['option']) || !is_array($vote['option']) || count($vote['option']) == 0) {
					throw new Exception(sprintf('"投票主题%d"，选项不能为空', $key), 400);
				}
				
				//保存投票主题信息
				$wechat_vote_theme = new entity('wechat_vote_theme');
				$wechat_vote_theme->creator = $wechat_vote->creator;
				$wechat_vote_theme->voteID = $voteID;
				$wechat_vote_theme->themeName = $vote['themeName'];
				$wechat_vote_theme->canMulti = $vote['canMulti'];
				$wechat_vote_theme->isMust = $vote['isMust'];
				$wechat_vote_theme->themeNum = 0;
				$this->dbca->save($wechat_vote_theme);
				$themeID = $this->dbca->insert_id;
				
				//保存投票选项信息
				$option_num = 0;
				foreach ($vote['option'] as &$option) {
					
					if (empty($option)) {
						continue;
					}
					
					$option_num++;
					//实例化
					$wechat_vote_theme_option = new entity('wechat_vote_theme_option');
					$wechat_vote_theme_option->creator = $wechat_vote->creator;
					$wechat_vote_theme_option->voteID = $voteID;
					$wechat_vote_theme_option->themeID = $themeID;
					$wechat_vote_theme_option->optionName = $option;
					$wechat_vote_theme_option->optionNum = 0;
					$wechat_vote_theme_option->optionSortby = $option_num;
					$this->dbca->save($wechat_vote_theme_option);
					
				}
				
				if ($option_num == 0) {
					throw new Exception(sprintf('"投票主题%d"，选项不能为空', $key), 400);
				}
				
			}

			$this->dbca->commit();
			
			return array('code' => 200,'message' => '保存成功');
			
		}catch(Exception $e){
			$this->dbca->rollback();
			if ($e->getCode() > 0) {
				return array('code' => $e->getCode(),'message' => $e->getMessage());
			} else {
				error_log($e->getMessage());
				return array('code' => 500,'message' => '系统错误');
			}
			
		}
	}
	
	/**
     * 修改问卷调查使用状态
     * $arg_array = array(
     * 		id => useFlg
     * )
     */
	public function changeVoteStatus($arg_array) {

		//参数检查
		if (!isset($arg_array['useFlg_str']) || $arg_array['useFlg_str'] == '') {
			return array( 'code' => 400, 'message' => '缺少参数' );
		}


		$str_array = explode('_', $arg_array['useFlg_str']);

		try{
			//检查投票信息是否存在或已经被操作
			$wechat_vote = new entity('wechat_vote');
			$wechat_vote->voteID = $str_array[0] ;
			if (!$this->dbca->fetch( $wechat_vote )){
				throw new Exception(sprintf('投票编号「%d」,的投票信息不存在,请审核后再操作',$str_array[0]) , 400) ;
			}

			//检查投票信息状态是否已经被修改
			if($wechat_vote->useFlg == $str_array[1] && $str_array[1] == 1){
				throw new Exception(sprintf('投票编号「%d」,的投票信息已启用',$str_array[0]) , 400) ;
			}

			if($wechat_vote->useFlg == $str_array[1] && $str_array[1] == 0){
				throw new Exception(sprintf('投票编号「%d」,的投票信息已禁用',$str_array[0]) , 400) ;
			}

			//保存
			$wechat_vote->useFlg = $str_array[1] ;
			$this->dbca->save( $wechat_vote);

		}catch (Exception $e) {
			if ($e->getCode() > 0){
				return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
			}else {
				error_log($e->getMessage());
				return array( 'code' => 500, 'message' => '系统错误' );
			}
		}

		if($str_array[1] == 1){
			return array( 'code' => 200, 'message' => '投票信息启用成功' );
		}else if ($str_array[1] == 0){
			return array( 'code' => 200, 'message' => '投票信息禁用成功' );
		}
	}
	
	/**
     * 审核投票信息
     * $arg_array = array(
     * 		id => useFlg
     * )
     */
	public function checkVoteStatus($arg_array) {

		//参数检查
		if (!isset($arg_array['status_str']) || $arg_array['status_str'] == '') {
			return array( 'code' => 400, 'message' => '缺少参数' );
		}


		$str_array = explode('_', $arg_array['status_str']);

		try{
			//检查投票信息是否存在或已经被操作
			$wechat_vote = new entity('wechat_vote');
			$wechat_vote->voteID = $str_array[0] ;
			
			if (!$this->dbca->fetch( $wechat_vote )){
				throw new Exception(sprintf('投票编号「%d」,的投票信息不存在,请审核后再操作',$str_array[0]) , 400) ;
			}
			
			if ($wechat_vote->useFlg == 0) {
				throw new Exception(sprintf('投票编号「%d」,的投票信息已禁用，不能审核',$str_array[0]) , 400) ;
			}

			//检查投票信息是否已经被修改
			if($wechat_vote->voteStatus == $str_array[1] && $str_array[1] == 1){
				throw new Exception(sprintf('投票编号「%d」,的投票信息已审核',$str_array[0]) , 400) ;
			}
			
			//保存
			$wechat_vote->voteStatus = 1 ;
			$this->dbca->save( $wechat_vote);

		}catch (Exception $e) {
			if ($e->getCode() > 0){
				return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
			}else {
				error_log($e->getMessage());
				return array( 'code' => 500, 'message' => '系统错误' );
			}
		}

		if($str_array[1] == 0){
			return array( 'code' => 200, 'message' => '投票信息审核成功' );
		}
	}
	
	/**
	 * 删除投票信息
	 */
	public function deleteVote( $arg_array ) {

		try{
			//开启问卷调查
			$this->dbca->beginTransaction();
			foreach($arg_array as $k){
				$wechat_vote = new entity('wechat_vote');
				$wechat_vote->voteID = $k;
				if($this->dbca->fetch($wechat_vote)){
					$this->dbca->delete($wechat_vote);
				}
				
				$del_theme	= $this->dbca->stmt_query('DELETE FROM wechat_vote_theme WHERE voteID=?','i',$k);
				$del_option	= $this->dbca->stmt_query('DELETE FROM wechat_vote_theme_option WHERE voteID=?','i',$k);
				
			}
			$this->dbca->commit();
			return array( 'code' => 200, 'message' => '删除成功' );

		}catch(Exception $e){
			$this->dbca->rollBack();
			error_log($e->getMessage());
		}
	}
	
	/**
     * 查询投票统计结果
     * @author raohenglei
     */
    public function searchVoteResult( &$arg_array ){

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //投票信息ID
        if ( isset($arg_array['voteID']) && is_numeric($arg_array['voteID']) ) {
            $sql .= ' AND voteID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['voteID'];
        }

        //投票主题ID
        if ( isset($arg_array['themeID']) && is_numeric($arg_array['themeID']) ) {
            $sql .= ' AND themeID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['themeID'];
        }

        if ( $sql ) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            //查询投票统计信息
            $search_sql = 'SELECT voteID,themeID,optionID,COUNT(optionID) as count FROM wechat_vote_cust_detail ' . $sql .' GROUP BY voteID,themeID,optionID';
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'voteID' 	  => $item->voteID,
                    'themeID' 	  => $item->themeID,
                    'optionID' 	  => $item->optionID,
                    'count' 	  => $item->count
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }
	
}